// 全局请求封装
import store from '@/store/index.js'
import config from '@/config/config.js'

export function uploadFilePromise(url) {
	let loginUser = store.state.user;
	const token = loginUser.token ? loginUser.token : '';
	let header = {
		AuthToken: token,
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.baseUrl + '/file/upload',
			filePath: url,
			header: header,
			name: 'file',
			success: (response) => {
				if (response.statusCode == '200' || response.status == '200') {
					let data = JSON.parse(response.data);
					if (data && data.code === 2000) {
						resolve(data);
					} else if (data && data.code === 5004) {
						uni.navigateTo({
							url: '/pages/me/login/login'
						})
					} else {
						reject(data);
					}
				} else {
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: '请求失败'
					});
					reject(data);
				}
			},
			fail(err) {
				uni.showToast({
					icon: 'error',
					position: 'bottom',
					title: '请求失败'
				});
				reject(res);
			},
		});
	})
}